package design.mode.effective_java.chapter7.item42;

import java.util.function.DoubleBinaryOperator;

/**
 * @author ZhongMingde on 2021/8/28
 */

public enum Operation {

    //    PLUS("+") {
//        public double apply(double x, double y) {
//            return x + y;
//        }
//    },
//    MINUS("-") {
//        public double apply(double x, double y) {
//            return x - y;
//        }
//    },
//
//    TIMES("*") {
//        public double apply(double x, double y) {
//            return x * y;
//        }
//    };
    PLUS("+", (x, y) -> x + y),
    MINUS("-", (x, y) -> x - y),
    TIMES("*", (x, y) -> x * y),
    DIVIDE("/", (x, y) -> x / y);

    private final String symbol;

    private final DoubleBinaryOperator op;

    Operation(String symbol, DoubleBinaryOperator op) {
        this.symbol = symbol;
        this.op = op;
    }

    @Override
    public String toString() {
        return symbol;
    }

    public double apply(double x, double y) {
        return op.applyAsDouble(x, y);
    }


}
